<template>
  <div class="dialog" v-show="modelValue" :class="{ fade: isFade }">
    <div class="wrapper" :class="{ fade: isFade }">
      <div class="header">
        <h3>{{ title }}</h3>
        <a
          @click="closeDialog()"
          href="JavaScript:;"
          class="iconfont icon-close-new"
        ></a>
      </div>
      <div class="body">
        <slot />
      </div>
      <div class="footer">
        <slot name="footer" />
      </div>
    </div>
  </div>
</template>

<script>
import { ref, watch } from 'vue'
import { move } from '@/utils/dialog'
export default {
  name: 'XtxDialog',
  props: {
    title: {
      type: String,
      default: ''
    },
    modelValue: {
      type: Boolean,
      default: false
    }
  },
  setup(props, { emit }) {
    const isFade = ref(false)

    // visible的值为true打开对话框，否则就是关闭对话框，其实控制fade的值即可
    watch(
      () => props.modelValue,
      (newVal) => {
        setTimeout(() => {
          isFade.value = newVal
        }, 0)
      },
      { immediate: true }
    )

    // 自己关闭对话框，修改父组件数据状态
    const closeDialog = () => {
      emit('update:modelValue', false)
      // 取消滑动限制-在关闭遮罩层的时候调用
      move()
    }

    return { isFade, closeDialog, stop, move }
  }
}
</script>
<style scoped lang="less">
.dialog {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 999999;
  background-color: rgba(0, 0, 0, 0);
  overflow: auto;
  &.fade {
    transition: al 0.4s;
    background-color: rgba(0, 0, 0, 0.5);
    .wrapper {
      transition: all 0.4s;
      transform: translate(-50%, -50%);
      opacity: 1;
    }
  }
  .wrapper {
    width: 600px;
    background: #fff;
    border-radius: 4px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    opacity: 0;
    .body {
      padding: 20px 40px;
      font-size: 16px;
      .icon-warning {
        color: @priceColor;
        margin-right: 3px;
        font-size: 16px;
      }
    }
    .footer {
      text-align: center;
      padding: 10px 0 30px 0;
    }
    .header {
      position: relative;
      height: 70px;
      line-height: 70px;
      padding: 0 20px;
      border-bottom: 1px solid #f5f5f5;
      h3 {
        font-weight: normal;
        font-size: 18px;
      }
      a {
        position: absolute;
        right: 25px;
        top: 25px;
        font-size: 24px;
        width: 20px;
        height: 20px;
        line-height: 20px;
        text-align: center;
        color: #999;
        &:hover {
          color: #666;
        }
      }
    }
  }
}
</style>
